package com.haiyi.activiti.engine.framework.activiti.manager.impl;

import com.haiyi.activiti.engine.model.entity.Role;
import com.haiyi.activiti.engine.framework.activiti.manager.RoleManager;
import com.haiyi.activiti.engine.framework.basic.DateTimeAdapter;
import com.haiyi.activiti.engine.framework.basic.IdBuilder;
import com.haiyi.activiti.engine.framework.basic.SecurityAdapter;
import com.haiyi.activiti.engine.query.RoleQuery;
import com.haiyi.activiti.engine.service.IRoleService;
import com.haiyi.activiti.engine.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p> </p>
 *
 * <pre> Created: 2018/12/5 16:21 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class RoleManagerImpl implements RoleManager {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IdBuilder idBuilder;
    @Autowired
    private SecurityAdapter securityAdapter;
    @Autowired
    private DateTimeAdapter dateTimeAdapter;

    @Override
    public void createRole(Role role) {
        if(role.getIid()==null){
            role.setIid(idBuilder.getId());
        }
        role.setCreater(securityAdapter.getCurrentUser().getIid());
        role.setCreateTime(dateTimeAdapter.getCurrentDate());
        Assert.isTrue(roleService.insert(role),"角色创建失败！");
    }

    @Override
    public void updateRole(Role role) {
        Assert.isNotNull(role.getIid(),"角色id不能为空！");
        role.setUpdater(securityAdapter.getCurrentUser().getIid());
        role.setUpdateTime(dateTimeAdapter.getCurrentDate());
        Assert.isTrue(roleService.updateById(role),"角色更新失败！");
    }

    @Override
    public void deleteRole(String roleId) {
        Assert.isNotNull(roleId,"角色id不能为空！");
        Assert.isTrue(roleService.deleteById(roleId),"角色删除失败！");
    }

    @Override
    public RoleQuery createRoleQuery() {
        final RoleQuery roleQuery = roleService.createQuery();
        Assert.isNotNull(roleQuery,"创建RoleQuery失败！");
        return roleQuery;
    }
}
